﻿using System.Windows.Forms;
using System.Drawing;
using BLL;

namespace GUI.Utilities
{
    public class ImageListBox : ListBox
    {
        public ImageListBox()
        {
            this.DrawMode = DrawMode.OwnerDrawVariable;
            this.ItemHeight = 20;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (e.Index >= this.Items.Count || e.Index <= -1)
                return;

            object item = this.Items[e.Index];

            if (item == null)
                return;

            ListBoxItem selected = (ListBoxItem)item;
            SolidBrush brush = new SolidBrush(Color.Black);

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), e.Bounds);
                brush.Color = Color.White;
            }
            else
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
            }

            string text = selected.Text;
            SizeF stringSize = e.Graphics.MeasureString(text, this.Font);
            PointF pointFlag = new PointF(5, e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2);
            PointF pointName = new PointF(22, pointFlag.Y);

            e.Graphics.DrawImage((Image) GUI.Properties.Resources.ResourceManager.GetObject("flag_" + selected.Image), pointFlag);
            e.Graphics.DrawString(text, this.Font, brush, pointName);
        }
    }

    public class ListBoxItem
    {
        public string Image { get; set; }
        public string Text { get; set; }

        public ListBoxItem(string Image, string Text)
        {
            this.Image = Image;
            this.Text = Text;
        }
    }
}